package com.example.activitytest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class SecondActivity extends BaseActivity {
    private static final String TAG = "SecondActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: Task id is " + getTaskId());
        setContentView(R.layout.activity_second);

        //显示之前缓存数据
        if (savedInstanceState != null){
            String data = savedInstanceState.getString("data_key");
            Log.d(TAG, "onCreate: " + data);
        }

        Intent intent = getIntent();
        String data = intent.getStringExtra("data_from_firstActivity");
        Log.d("SecondActivity",data);
        Button back = (Button) findViewById(R.id.back1);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent.putExtra("data_return","return message from second activity.");
                setResult(RESULT_OK,intent);
                Toast.makeText(SecondActivity.this, "已经返回结果", Toast.LENGTH_SHORT).show();
                finish();
            }
        });

        Button startNormalActivity = (Button) findViewById(R.id.button4);
        Button startDialogActivity = (Button) findViewById(R.id.button5);
        startNormalActivity.setOnClickListener(view -> {
            Intent intent1 = new Intent(SecondActivity.this,NormalActivity.class);
            startActivity(intent1);
        });
        startDialogActivity.setOnClickListener(view -> {
            Intent intent2 = new Intent(SecondActivity.this,DialogActivity.class);
            startActivity(intent2);
        });

        Button thirdActivity = (Button) findViewById(R.id.button3);
        thirdActivity.setOnClickListener(view -> {
            Intent intent2 = new Intent(SecondActivity.this,ThirdActivity.class);
            startActivity(intent2);
        });
    }

    //重写系统返回键
    @Override
    public void onBackPressed() {
        Intent intent = getIntent();
        intent.putExtra("data_return","return message from second activity.");
        setResult(RESULT_OK,intent);
        Toast.makeText(SecondActivity.this, "已经返回结果", Toast.LENGTH_SHORT).show();
        finish();
    }

    //临时数据保存
    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        String tempData = "Something you just typed";
        outState.putString("data_key",tempData);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }
}
